# -*- coding: utf-8 -*-
import scrapy
import urllib.parse as parse_url
import re
from ..items import TaobaoItem
import time
from lxml import etree


class TaobaoSpider(scrapy.Spider):
    name = 'Taobao'
    allowed_domains = ['taobao.com']

    def start_requests(self):
        url = [
            'https://item.taobao.com/item.htm?spm=a230r.1.14.69.fac11611mqkKBW&id=570526223024&ns=1&abbucket=2#detail',
            'https://item.taobao.com/item.htm?spm=a230r.1.14.90.fac11611mqkKBW&id=584796286566&ns=1&abbucket=2#detail',
            'https://item.taobao.com/item.htm?spm=a230r.1.14.41.450f4ddeofSbOf&id=552448450623&ns=1&abbucket=3#detail',
            'https://item.taobao.com/item.htm?spm=a230r.1.14.92.3a2847d228IXWl&id=577165445501&ns=1&abbucket=3#detail',
            'https://item.taobao.com/item.htm?spm=a230r.1.14.85.3a2847d228IXWl&id=565558602373&ns=1&abbucket=3#detail',
            'https://item.taobao.com/item.htm?spm=a230r.1.14.161.3a2847d228IXWl&id=592964698467&ns=1&abbucket=3#detail',
            'https://item.taobao.com/item.htm?spm=a230r.1.14.188.1e8f2581FkkQVY&id=525073557931&ns=1&abbucket=3#detail',
            'https://item.taobao.com/item.htm?spm=a230r.1.14.30.1e8f2581FkkQVY&id=559327733096&ns=1&abbucket=3#detail',
            'https://item.taobao.com/item.htm?spm=a230r.1.14.142.1e8f2581FkkQVY&id=21992427145&ns=1&abbucket=3#detail',
            'https://item.taobao.com/item.htm?id=596208284328&ali_refid=a3_430585_1006:1102994845:N:43O%2Ffhy6XbsM%2FvDs4a6dug%3D%3D:79144bea7e95ad59fd3496282556d562&ali_trackid=1_79144bea7e95ad59fd3496282556d562&spm=a230r.1.14.11#detail',
        ]
        for i in url:
            yield scrapy.Request(
                url=i
            )

    def parse(self, response):
        pattern = re.compile(r'(\S+)', re.S)  # 正则解析语句
        ca_pattern = re.compile(r'<customSegmentation>(.*?)</customSegmentation>', re.S)
        category = ca_pattern.findall(response.text)
        title = response.xpath('//*[@id="J_Title"]/h3/text()').extract()  # 商品名称
        title = pattern.findall(title[0])
        shop = response.xpath('//*[@id="J_ShopInfo"]/div/div[1]/div[1]/dl/dd/strong/a/text()').extract()  # 商品店家
        shop = pattern.findall(shop[0])
        shop_owner = response.xpath('//*[@id="J_ShopInfo"]/div/div[1]/div[3]/dl/dd/a/text()').extract()  # 店家主名称
        shop_owner = pattern.findall(shop_owner[0])
        sort1 = ''
        sort2 = ''
        price = ''
        classification_price = []
        for i in category:
            parse_html = etree.HTML(i)
            sort1 = parse_html.xpath('//*[@id="J_isku"]/div/dl[1]/dd/ul/li[@class="tb-selected"]/a/span/text()')
            sort2 = parse_html.xpath('//*[@id="J_isku"]/div/dl[2]/dd/ul/li[@class="tb-selected"]/a/span/text()')
            sort1 = self.is_empty(sort1)
            sort2 = self.is_empty(sort2)
            price = parse_html.xpath('//*[@id="J_PromoPriceNum"]/text()')
            classification_price.append((sort1, sort2, float(price[0])))
        title = self.format_string(title)
        shop = self.format_string(shop)
        shop_owner = self.format_string(shop_owner)
        url = response.url  # 店家url
        # 创建item对象
        item = TaobaoItem()
        item['price'] = classification_price
        item['title'] = title
        item['shop'] = shop
        item['shop_owner'] = shop_owner
        item['url'] = url
        item['time'] = time.strftime('%Y-%m-%d %H:%M:%S')
        # 抛出item
        yield item

    @staticmethod
    def is_empty(sort1):
        """
        判断里列表是否为空，为空则赋值为空字符串，不为空，提取第一个
        :param sort1:
        :return:
        """
        if len(sort1) == 0:
            sort1 = ""
        else:
            sort1 = str(sort1[0])
        return sort1

    @staticmethod
    def format_string(required):
        """
        格式化字符串，去掉空行，空格，并且将淘宝原本字与字之间的空格保留下来
        :param required: str类型，待格式化的字符串
        :return: str类型，格式化完成的字符串
        """
        result = ""
        for i in required:
            result += i + " "
        result = result[:-1]
        return result
